#include "mainwindow.h"

#include <QtGui/QDesktopWidget>
#include <QtGui/QIcon>

#include <algorithm>
#include <iostream>

namespace verema
{

MainWindow::MainWindow()
: wdg_helper_widget()
, vbl_mainlayout(&wdg_helper_widget)
, m_mainMenu(this)
, m_viewGroup(this)
, m_statusbar(this)
, m_dataWidget()
, m_visualWidget()
, m_configWidget()
{
	setMinimumSize(800,600);
	setWindowTitle("Verema - Visual Environment and Resource Manager");

	QDesktopWidget screen;
	int xMiddle = screen.width()/2;
	int yMiddle = screen.height()/2;
	move(xMiddle - (width()/2), yMiddle - (height()/2));

    QWidget::setWindowIcon(QIcon("va.png"));

	m_dataWidget.hide();
	m_dataWidget.setParent(&wdg_helper_widget);

	m_visualWidget.hide();
	m_visualWidget.setParent(/*&wdg_helper_widget*/this);

	//Menüerstellung
	m_fileMenu = m_mainMenu.addMenu("&Datei");
	m_viewMenu = m_mainMenu.addMenu("&Ansicht");

	m_preferencesMenu = m_mainMenu.addMenu("&Konfiguration");
	m_preferencesPG = m_preferencesMenu->addAction("Programm");
	m_preferencesDB = m_preferencesMenu->addAction("Datenbank");

	m_helpMenu = m_mainMenu.addMenu("&Hilfe");
	m_newData = m_fileMenu->addAction("Neue Datenbank erstellen");
	m_openData = m_fileMenu->addAction(QString::fromUtf8("Vorhandene Datenbank öffnen"));
	//m_fileMenu->addSeparator();
	//m_saveData = m_fileMenu->addAction("Datenbank speichern");
	//m_saveDataAs = m_fileMenu->addAction("Datenbank speichern unter...");
	m_fileMenu->addSeparator();
	m_exit = m_fileMenu->addAction(QString::fromUtf8("Verema schließen"));
	m_visualView = m_viewMenu->addAction("Visuelle Ansicht");
	m_dataView = m_viewMenu->addAction("Datenbank Ansicht");
	m_documentation = m_helpMenu->addAction("Benutzerdokumentation");
	m_about = m_helpMenu->addAction("Version");

	setMenuBar(&m_mainMenu);

	//View Auswahl
	m_visualView->setCheckable(true);
	m_dataView->setCheckable(true);
	m_visualView->setActionGroup(&m_viewGroup);
	m_dataView->setActionGroup(&m_viewGroup);

	Config* m_config = Config::getInstance();

	if(m_config->view() == "DataView")
	{
        m_visualView->setChecked(false);
        m_dataView->setChecked(true);
        dataView(true);
	}
	else
	{
        m_visualView->setChecked(true);
        m_dataView->setChecked(false);
        dataView(false);
	}

	//Statusbar
	//m_statusbar.showMessage("Hier werden immer mal wichtige Infos angezeigt");
	setStatusBar(&m_statusbar);

	//Connections
	connect(m_exit, SIGNAL(triggered()), this, SLOT(close()));
	connect(m_preferencesPG, SIGNAL(triggered()), this, SLOT(configWidgetPG()));
	connect(m_preferencesDB, SIGNAL(triggered()), this, SLOT(configWidgetDB()));
	connect(m_dataView, SIGNAL(toggled(bool)), this, SLOT(dataView(bool)));
	connect(m_newData, SIGNAL(triggered()), this, SLOT(newDatabase()));
	connect(&m_configWidget, SIGNAL(configChanged()), this, SLOT(loadConfig()));
	connect(m_openData, SIGNAL(triggered()), this, SLOT(loadDatabase()));

	vbl_mainlayout.addWidget(&m_visualWidget);
	vbl_mainlayout.addWidget(&m_dataWidget);

	setCentralWidget(&wdg_helper_widget);

	m_visualWidget.setStatusBar(&m_statusbar);
}

MainWindow::~MainWindow()
{
}

void MainWindow::dataView(bool checked)
{
	if(checked == true)
	{
		m_visualWidget.hide();
		m_dataWidget.show();
	}
	else
	{
		m_dataWidget.hide();
		m_visualWidget.show();
	}
}

void MainWindow::configWidgetDB()
{
    m_configWidget.mainWindowPosition(pos());
    m_configWidget.show(0);
    m_configWidget.activateWindow();
}

void MainWindow::configWidgetPG()
{
    m_configWidget.mainWindowPosition(pos());
    m_configWidget.show(1);
    m_configWidget.activateWindow();
}

void MainWindow::reloadWidgets()
{
    m_configWidget.reloadData();

    if(m_visualWidget.isVisible() == true)
    {
        m_visualWidget.hide();
        m_visualWidget.show();
    }
    else
    {
        m_dataWidget.hide();
        m_dataWidget.show();
    }
}

void MainWindow::newDatabase()
{
	QString filepath = QFileDialog::getSaveFileName(this, "Neue Datenbank speichern unter...", "","Datenbanken (*.db)");
    if(!filepath.isEmpty())
    {

        if(filepath.size() > 3)
        {
            if( filepath.toStdString().substr( filepath.size() - 3) != ".db")
            {
                filepath.append(".db");
            }
        }
        else
        {
            filepath.append(".db");
        }

        std::string filename = filepath.toStdString();
        filename = filename.substr(filename.find_last_of('/')+1);

        std::string temp_filepath = filepath.toStdString();
        std::replace(temp_filepath.begin() , temp_filepath.end(), '\\', '/' );
        filepath = temp_filepath.c_str();

        m_dataWidget.addNewDatabase(filepath);
        Config::getInstance()->dbname(filename);

        reloadWidgets();
    }
}

void MainWindow::loadConfig()
{
    reloadWidgets();
}

void MainWindow::loadDatabase()
{
    QString filepath = QFileDialog::getOpenFileName(this, "Bitte Datenbank wählen...", "", "Datenbanken (*.db)");
    if(!filepath.isEmpty())
    {
        std::string filename = filepath.toStdString();
        filename = filename.substr(filename.find_last_of('/')+1);

        std::string temp_filepath = filepath.toStdString();
        std::replace(temp_filepath.begin() , temp_filepath.end(), '\\', '/' );
        filepath = temp_filepath.c_str();
        Config::getInstance()->dbname(filename);

        reloadWidgets();
    }
}

} // namespace verema
